package io.youngledo.usb.connection;

import io.youngledo.usb.storage.ObjectStorageService;
import io.youngledo.usb.storage.StorageConfig;

/**
 * 存储连接封装类
 * 包含连接的配置、服务实例和状态信息
 */
public class StorageConnection {
    private final String id;
    private final String displayName;
    private final StorageConfig config;
    private ObjectStorageService service;
    private boolean isConnected;
    private String currentBucket;
    private String currentPrefix;
    private boolean isSavedConnection;

    public StorageConnection(String id, String displayName, StorageConfig config) {
        this.id = id;
        this.displayName = displayName;
        this.config = config;
        this.isConnected = false;
        this.currentBucket = null;
        this.currentPrefix = "";
    }

    // Getters
    public String getId() {
        return id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public StorageConfig getConfig() {
        return config;
    }

    public ObjectStorageService getService() {
        return service;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public String getCurrentBucket() {
        return currentBucket;
    }

    public String getCurrentPrefix() {
        return currentPrefix;
    }

    public boolean isSavedConnection() {
        return isSavedConnection;
    }

    // Setters
    public void setService(ObjectStorageService service) {
        this.service = service;
    }

    public void setConnected(boolean connected) {
        this.isConnected = connected;
    }

    public void setCurrentBucket(String currentBucket) {
        this.currentBucket = currentBucket;
    }

    public void setCurrentPrefix(String currentPrefix) {
        this.currentPrefix = currentPrefix;
    }

    public void setSavedConnection(boolean savedConnection) {
        this.isSavedConnection = savedConnection;
    }

    /**
     * 断开连接并重置状态
     */
    public void disconnect() {
        this.service = null;
        this.isConnected = false;
        this.currentBucket = null;
        this.currentPrefix = "";
    }

    /**
     * 获取连接的简短描述
     */
    public String getConnectionInfo() {
        return config.type().getDisplayName() + " (" + config.endpoint() + ")";
    }

    @Override
    public String toString() {
        return displayName + " [" + (isConnected ? "已连接" : "未连接") + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        StorageConnection that = (StorageConnection) obj;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}